package com.qigao.mall.service.impl;

import com.qigao.mall.api.ShopEmpService;
import com.qigao.mall.commons.dto.ShopEmpCreateDto;
import com.qigao.mall.commons.dto.ShopEmpResultDto;
import com.qigao.mall.jdbc.mapper.TbShopEmpMapper;
import com.qigao.mall.jdbc.model.TbShopEmp;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShopEmpServiceImpl implements ShopEmpService {
    @Resource
    private TbShopEmpMapper tbShopEmpMapper;
    @Override
    public void create(ShopEmpCreateDto shopEmpCreateDto) {
        TbShopEmp tbShopEmp=new TbShopEmp();
        BeanUtils.copyProperties(shopEmpCreateDto,tbShopEmp);
        tbShopEmp.setCreateNo(shopEmpCreateDto.getOperNo());
        tbShopEmp.setCreateTime(new Date());
        tbShopEmpMapper.insertSelective(tbShopEmp);
    }

    @Override
    public void enableDisable(Long id, String currentUserNo) {
        TbShopEmp tbShopEmp=new TbShopEmp();
        tbShopEmp.setId(id);
        tbShopEmp.setModiNo(currentUserNo);
        tbShopEmp.setModiTime(new Date());
        tbShopEmpMapper.enableDisable(tbShopEmp);
    }

    @Override
    public List<ShopEmpResultDto> findListByShopNo(String shopNo) {
        List<TbShopEmp> tbShopEmps= tbShopEmpMapper.selectByShopNo(shopNo);
        List<ShopEmpResultDto> shopEmpResultDtos=new ArrayList<>();
        if(tbShopEmps!=null && tbShopEmps.size()>=1){
            for(TbShopEmp tbShopEmp:tbShopEmps){
                ShopEmpResultDto shopEmpResultDto=new ShopEmpResultDto();
                BeanUtils.copyProperties(tbShopEmp,shopEmpResultDto);
                shopEmpResultDtos.add(shopEmpResultDto);
            }
        }
        return shopEmpResultDtos;
    }

    @Override
    public List<ShopEmpResultDto> findByEmpNo(String empNo) {
        List<TbShopEmp> tbShopEmps= tbShopEmpMapper.selectByEmpNo(empNo);
        List<ShopEmpResultDto> shopEmpResultDtos=new ArrayList<>();
        if(tbShopEmps!=null && tbShopEmps.size()>=1){
            for(TbShopEmp tbShopEmp:tbShopEmps){
                ShopEmpResultDto shopEmpResultDto=new ShopEmpResultDto();
                BeanUtils.copyProperties(tbShopEmp,shopEmpResultDto);
                shopEmpResultDtos.add(shopEmpResultDto);
            }
        }
        return shopEmpResultDtos;
    }

    @Override
    public boolean ifShopEmp(String shopNo, String userNo) {
        int countNum = tbShopEmpMapper.countByShopNoAndEmpNo(shopNo,userNo);
        if(countNum>=1){
            return true;
        }
        return false;
    }
}
